package com.example.yeujian5.yuejian5.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yeujian5.yuejian5.Listenner.OrderListener;
import com.example.yeujian5.yuejian5.R;
import com.example.yeujian5.yuejian5.model.TrainModel;

public class TrainOrderActivity extends AppCompatActivity implements OrderListener,View.OnClickListener{

        private RadioGroup rg;
        private RadioButton WeChat,Alipay;
        private ImageButton add,reduce;
        private TextView name,username,price,tel,intro,allprice;
        private EditText et_num;
        private Button buy;
        private ImageView reg_return;
        private int num=1,userid;
        String trname,trprice,notice,telephone,uname,Pay_type,Aprice,Anum,train_id;
        SharedPreferences sp;
        Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_train_order);
        context=TrainOrderActivity.this;
        initViews();
        reg_return.setOnClickListener(this);
        add.setOnClickListener(this);
        reduce.setOnClickListener(this);
        buy.setOnClickListener(this);

        }



public void initViews() {
        /*获取用户*/
        sp=context.getSharedPreferences("Userid",MODE_PRIVATE);
        userid=Integer.valueOf(sp.getString("user_id",null));
        telephone=sp.getString("user_tel",null);
        uname=sp.getString("user_name",null);
        /*获取活动名*/
        Intent intent=getIntent();
        train_id=intent.getStringExtra("train_id");
        trname=intent.getStringExtra("train_name");
        trprice=intent.getStringExtra("train_price");
        notice=intent.getStringExtra("train_notice");
        name=(TextView)findViewById(R.id.name);
        username= (TextView) findViewById(R.id.username);
        price= (TextView) findViewById(R.id.price);
        tel= (TextView) findViewById(R.id.tel);
        intro= (TextView) findViewById(R.id.intro);
        allprice= (TextView) findViewById(R.id.count);
        et_num=(EditText)findViewById(R.id.num) ;
        add=(ImageButton)findViewById(R.id.add);
        reduce=(ImageButton)findViewById(R.id.reduce);
        buy= (Button) findViewById(R.id.buy);
        rg=(RadioGroup) findViewById(R.id.radiogroup);
        WeChat=(RadioButton) findViewById(R.id.W_pay);
        Alipay=(RadioButton) findViewById(R.id.Z_pay);
        reg_return= (ImageView) findViewById(R.id.reg_return);
        name.setText(trname);
        price.setText(trprice);
        intro.setText(notice);
        tel.setText(telephone);
        username.setText(uname);

        allprice.setText((num*Integer.parseInt(trprice))+"(元)");
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                        switch (i){
                                case R.id.W_pay:
                                        Pay_type="微信支付";
                                        break;
                                case R.id.Z_pay:
                                        Pay_type="支付宝支付";
                                        break;
                        }
                }
        });
        }

@Override
public void onClick(View v) {
        switch (v.getId()){


        /*结算*/
        case R.id.buy:
                count();
                break;


         /*加*/
        case R.id.add:
                        doAdd();
                        break;
        /*减*/
        case R.id.reduce:
                        doReduce();
                        break;



        case R.id.reg_return:
                finish();



        }
    }


    //结算
        private void count(){
                trname = name.getText().toString();
                telephone = tel.getText().toString();
                Anum = et_num.getText().toString();
                Aprice=allprice.getText().toString();
                TrainModel trainModel=new TrainModel();
                trainModel.signTrain(trname,train_id,telephone,Anum,Pay_type,Aprice,notice,userid,this);


        }


        /*减*/
    private void doReduce() {

                num=Integer.valueOf(et_num.getText().toString());
                if (0!=num)
                        num--;
                et_num.setText(num+"");
            allprice.setText((num*Integer.parseInt(trprice))+"(元)");

        }

        /*加*/
    private void doAdd() {
                num=Integer.valueOf(et_num.getText().toString());
                num++;
                et_num.setText(num+"");
            allprice.setText((num*Integer.parseInt(trprice))+"(元)");
        }

        @Override
        public void onResponse(Integer bean) {
                Toast.makeText(TrainOrderActivity.this,"下单成功",Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onFail(String msg) {
                Toast.makeText(TrainOrderActivity.this,"下单失败",Toast.LENGTH_SHORT).show();

        }
}
